package com.vca.service.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vca.common.constants.CategoryConstants;
import com.vca.common.constants.Constants;
import com.vca.common.exception.VcaException;
import com.vca.common.model.category.Category;
import com.vca.common.model.exhibition.ExhibitionScheduling;
import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.CourseTypeResponse;
import com.vca.common.response.ProductTypeAdminResponse;
import com.vca.common.utils.VcaUtil;
import com.vca.common.vo.CategoryTreeVo;
import com.vca.common.vo.CourseTypeVo;
import com.vca.service.dao.category.CategoryDao;
import com.vca.service.service.CategoryService;
import com.vca.service.service.SystemAttachmentService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * CategoryServiceImpl 接口实现
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Resource
    private CategoryDao dao;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private TransactionTemplate transactionTemplate;


    /**
     * 获取分类下子类的数量
     *
     * @param request          请求参数
     * @param pageParamRequest 分页参数
     * @return List<Category>
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public List<Category> getList(CategorySearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (null != request.getPid()) {
            lambdaQueryWrapper.eq(Category::getPid, request.getPid());
        }
        if (null != request.getType()) {
            lambdaQueryWrapper.eq(Category::getType, request.getType());
        }
        if (ObjectUtil.isNotNull(request.getStatus()) && request.getStatus() >= 0) {
            lambdaQueryWrapper.eq(Category::getStatus, request.getStatus().equals(CategoryConstants.CATEGORY_STATUS_NORMAL));
        }
        if (null != request.getName()) {
            lambdaQueryWrapper.like(Category::getName, request.getName());
        }
        lambdaQueryWrapper.orderByDesc(Category::getSort).orderByDesc(Category::getId);
        return dao.selectList(lambdaQueryWrapper);
    }

    /**
     * 通过id集合获取列表
     *
     * @param idList List<Integer> id集合
     * @return List<Category>
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public List<Category> getByIds(List<Integer> idList) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Category::getId, idList);
        return dao.selectList(lambdaQueryWrapper);
    }

    /**
     * 通过id集合获取列表 id => name
     *
     * @param cateIdList List<Integer> id集合
     * @return HashMap<Integer, String>
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public HashMap<Integer, String> getListInId(List<Integer> cateIdList) {
        HashMap<Integer, String> map = new HashMap<>();
        List<Category> list = getByIds(cateIdList);
        for (Category category : list) {
            map.put(category.getId(), category.getName());
        }

        return map;
    }

    /**
     * 查询id和url是否存在
     *
     * @return Boolean
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public Boolean checkAuth(List<Integer> pathIdList, String uri) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Category::getId, pathIdList).eq(Category::getUrl, uri);
        List<Category> categoryList = dao.selectList(lambdaQueryWrapper);
        if (categoryList.size() < 1) {
            return false;
        }

        return true;
    }

    /**
     * 修改
     *
     * @param request CategoryRequest
     * @param id      Integer
     * @return bool
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public boolean update(CategoryRequest request, Integer id) {
        try {
            //修改分类信息
            Category category = new Category();
            BeanUtils.copyProperties(request, category);
            category.setId(id);
            category.setPath(getPathByPId(category.getPid()));

            updateById(category);

            //如状态为关闭，那么所以子集的状态都关闭
            if (!request.getStatus()) {
                updateStatusByPid(id, false);
            } else {
                //如是开启，则父类的状态为开启
                updatePidStatusById(id);
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 开启父级状态
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    private void updatePidStatusById(Integer id) {
        Category category = getById(id);
        List<Integer> categoryIdList = VcaUtil.stringToArrayByRegex(category.getPath(), "/");
        categoryIdList.removeIf(i -> i.equals(0));
        ArrayList<Category> categoryArrayList = new ArrayList<>();
        if (categoryIdList.size() < 1) {
            return;
        }
        for (Integer categoryId : categoryIdList) {
            Category categoryVo = new Category();
            categoryVo.setId(categoryId);
            categoryVo.setStatus(true);
            categoryArrayList.add(categoryVo);
        }
        updateBatchById(categoryArrayList);
    }

    /**
     * 获取分类下子类的数量
     *
     * @param pid Integer
     * @return bool
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    private int getChildCountByPid(Integer pid) {
        //查看是否有子类
        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("path", "/" + pid + "/");
        return dao.selectCount(objectQueryWrapper);
    }

    /**
     * 修改分类以及子类的状态
     *
     * @param pid Integer
     * @return bool
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    private int updateStatusByPid(Integer pid, boolean status) {
        //查看是否有子类
        Category category = new Category();
        category.setStatus(status);

        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("path", "/" + pid + "/");
        return dao.update(category, objectQueryWrapper);
    }

    private String getPathByPId(Integer pid) {
        Category category = getById(pid);
        if (null != category) {
            return category.getPath() + pid + "/";
        }
        return null;
    }

    /**
     * 带结构的无线级分类
     *
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public List<CategoryTreeVo> getListTree(Integer type, Integer status, String name) {
        return getTree(type, status, name, null);
    }

    /**
     * 带权限的属性结构
     */
    @Override
    public List<CategoryTreeVo> getListTree(Integer type, Integer status, List<Integer> categoryIdList) {
        System.out.println("菜单列表:getListTree: type:" + type + "| status:" + status + "| categoryIdList:" + JSON.toJSONString(categoryIdList));
        return getTree(type, status, null, categoryIdList);
    }

    /**
     * 带结构的无线级分类
     *
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    private List<CategoryTreeVo> getTree(Integer type, Integer status, String name, List<Integer> categoryIdList) {
        //循环数据，把数据对象变成带list结构的vo
        List<CategoryTreeVo> treeList = new ArrayList<>();

        LambdaQueryWrapper<Category> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Category::getType, type);

        if (null != categoryIdList && categoryIdList.size() > 0) {
            lambdaQueryWrapper.in(Category::getId, categoryIdList);
        }

        if (status >= 0) {
            lambdaQueryWrapper.eq(Category::getStatus, status);
        }
        if (StringUtils.isNotBlank(name)) { // 根据名称模糊搜索
            lambdaQueryWrapper.like(Category::getName, name);
        }

        lambdaQueryWrapper.orderByDesc(Category::getSort);
        lambdaQueryWrapper.orderByAsc(Category::getId);
        List<Category> allTree = dao.selectList(lambdaQueryWrapper);
        if (allTree == null) {
            return null;
        }
        // 根据名称搜索特殊处理 这里仅仅处理两层搜索后有子父级关系的数据
        if (StringUtils.isNotBlank(name) && allTree.size() > 0) {
            List<Category> searchCategory = new ArrayList<>();
            List<Integer> categoryIds = allTree.stream().map(Category::getId).collect(Collectors.toList());

            List<Integer> pidList = allTree.stream().filter(c -> c.getPid() > 0 && !categoryIds.contains(c.getPid()))
                    .map(Category::getPid).distinct().collect(Collectors.toList());
            if (CollUtil.isNotEmpty(pidList)) {
                pidList.forEach(pid -> {
                    searchCategory.add(dao.selectById(pid));
                });
            }
            allTree.addAll(searchCategory);
        }

        for (Category category : allTree) {
            CategoryTreeVo categoryTreeVo = new CategoryTreeVo();
            BeanUtils.copyProperties(category, categoryTreeVo);
            treeList.add(categoryTreeVo);
        }


        //返回
        Map<Integer, CategoryTreeVo> map = new HashMap<>();
        //ID 为 key 存储到map 中
        for (CategoryTreeVo categoryTreeVo1 : treeList) {
            map.put(categoryTreeVo1.getId(), categoryTreeVo1);
        }

        List<CategoryTreeVo> list = new ArrayList<>();
        for (CategoryTreeVo tree : treeList) {
            //子集ID返回对象，有则添加。
            CategoryTreeVo tree1 = map.get(tree.getPid());
            if (tree1 != null) {
                tree1.getChild().add(tree);
            } else {
                list.add(tree);
            }
        }
        System.out.println("无限极分类 : getTree:" + JSON.toJSONString(list));
        return list;
    }

    /**
     * 删除分类表
     *
     * @param id Integer
     * @return bool
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public int delete(Integer id) {
        //查看是否有子类, 物理删除
        if (getChildCountByPid(id) > 0) {
            throw new VcaException("当前分类下有子类，请先删除子类！");
        }

        return dao.deleteById(id);
    }

    /**
     * 获取分类下子类
     *
     * @param pid Integer
     * @return List<Category>
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public List<Category> getChildVoListByPid(Integer pid) {
        //查看是否有子类
        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("status", CategoryConstants.CATEGORY_STATUS_NORMAL);
        objectQueryWrapper.like("path", "/" + pid + "/");
        return dao.selectList(objectQueryWrapper);
    }

    /**
     * 检测分类名称是否存在
     *
     * @param name String 分类名
     * @param type int 类型
     * @return int
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    private int checkName(String name, Integer type) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getName, name);
        lambdaQueryWrapper.eq(Category::getIsDel,0);
        if (ObjectUtil.isNotNull(type)) {
            lambdaQueryWrapper.eq(Category::getType, type);
        }
        return dao.selectCount(lambdaQueryWrapper);
    }

    /**
     * 检测url是否存在
     *
     * @param uri String url
     * @return int
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @Override
    public boolean checkUrl(String uri) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getUrl, uri);
        return dao.selectCount(lambdaQueryWrapper) > 0;
    }

    @Override
    public boolean updateStatus(Integer id) {
        Category category = getById(id);
        category.setStatus(!category.getStatus());
        return updateById(category);
    }

    /**
     * 新增分类
     *
     * @param categoryRequest
     */
    @Override
    public Boolean create(CategoryRequest categoryRequest) {
        //检测标题是否存在
        if (checkName(categoryRequest.getName(), categoryRequest.getType()) > 0) {
            throw new VcaException("此分类已存在");
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryRequest, category);
        category.setPath(getPathByPId(category.getPid()));
        category.setExtra(systemAttachmentService.clearPrefix(category.getExtra()));
        return save(category);
    }

    /**
     * 获取文章分类列表
     *
     * @return List<Category>
     */
    @Override
    public List<Category> findArticleCategoryList() {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.select(Category::getId, Category::getName);
        lambdaQueryWrapper.eq(Category::getType, Constants.CATEGORY_TYPE_ARTICLE);
        lambdaQueryWrapper.eq(Category::getStatus, true);
        lambdaQueryWrapper.orderByDesc(Category::getSort);
        lambdaQueryWrapper.orderByAsc(Category::getId);
        return dao.selectList(lambdaQueryWrapper);
    }

    /**
     * @Description:课程类型分页列表
     * @Author: chenBing
     * @Date: 2022/11/2
     */
    @Override
    public CommonPage<CourseTypeResponse> courseTypeList(Integer status, String courseTypeName, PageParamRequest pageParamRequest) {
        Page<Object> startPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<Category>()
                .eq(Category::getType, 8)
                .ne(Category::getPid, 0)
                .eq(Category::getIsDel,0)
                .orderByDesc(Category::getSort);
        if (ObjectUtil.isNotEmpty(status)){
            lqw.eq(Category::getStatus, status);
        }
        if (StringUtils.isNotBlank(courseTypeName)) {
            lqw.like(Category::getName, courseTypeName).or().like(Category::getNameEn, courseTypeName);
        }
        List<Category> categories = list(lqw);
        List<CourseTypeResponse> courseTypeResponseList = new ArrayList<>();
        categories.forEach(c -> {
            CourseTypeResponse courseTypeResponse = new CourseTypeResponse();
            courseTypeResponse.setId(Long.valueOf(c.getId()));
            courseTypeResponse.setCourseTypeName(c.getName());
            courseTypeResponse.setCourseTypeNameEn(c.getNameEn());
            courseTypeResponse.setStatus(c.getStatus());
            courseTypeResponse.setLabelColor(c.getLabelColor());
            courseTypeResponse.setSort(c.getSort());
            courseTypeResponse.setPid(Long.valueOf(c.getPid()));
            courseTypeResponseList.add(courseTypeResponse);
        });
        return CommonPage.restPage(CommonPage.copyPageInfo(startPage, courseTypeResponseList));
    }

    /**
     * @param status
     * @param courseTypeName
     * @param pageParamRequest
     * @Description:获取tree结构的列表
     * @Author: chenBing
     * @Date: 2022/11/2
     */
    @Override
    public List<CourseTypeVo> getCourseTypeListTree(Integer status, String courseTypeName, PageParamRequest pageParamRequest) {
        List<CourseTypeVo> courseTypeVos = new ArrayList<>();
        Category category = getOne(new LambdaQueryWrapper<Category>().eq(Category::getPid, 0).eq(Category::getType, 8));
        if (category == null) {
            return courseTypeVos;
        }
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<Category>()
                .eq(Category::getType, 8)
                .eq(Category::getPid, category.getId())
                .orderByAsc(Category::getSort);
        switch (status) {
            case 0:
            case 1:
                lqw.eq(Category::getStatus, status);
                break;
            case 2:
            default:
                break;
        }
        if (StringUtils.isNotBlank(courseTypeName)) {
            lqw.like(Category::getName, courseTypeName);
        }
        List<Category> categories = list(lqw);
        if (categories == null || categories.size() == 0) {
            return courseTypeVos;
        }
        //根据名称搜索进行特殊处理 这里仅仅处理两层搜索后有子级关系的数据
        if (categories.size() > 0) {
            List<Category> courseTypes = new ArrayList<>();
            List<Integer> courseTypeIds = categories.stream().map(Category::getId).collect(Collectors.toList());

            List<Integer> pidList = categories.stream().filter(courseType -> courseType.getPid() > 0 && !courseTypeIds.contains(courseType.getPid()))
                    .map(Category::getPid).distinct().collect(Collectors.toList());
            if (CollUtil.isNotEmpty(pidList)) {
                pidList.forEach(pid -> {
                    courseTypes.add(dao.selectById(pid));
                });
            }
            categories.addAll(courseTypes);
        }
        for (Category courseType : categories) {
            CourseTypeVo courseTypeVo = new CourseTypeVo();
            courseTypeVo.setId(Long.valueOf(courseType.getId()));
            courseTypeVo.setCourseTypeName(courseType.getName());
            courseTypeVo.setStatus(courseType.getStatus());
            courseTypeVo.setLabelColor(courseType.getLabelColor());
            courseTypeVo.setSort(courseType.getSort());
            courseTypeVo.setPid(Long.valueOf(courseType.getPid()));
            courseTypeVos.add(courseTypeVo);
        }
        Map<Long, CourseTypeVo> map = new HashMap<>();
        for (CourseTypeVo courseTypeVo : courseTypeVos) {
            map.put(courseTypeVo.getId(), courseTypeVo);
        }

        List<CourseTypeVo> courseTypeVoList = new ArrayList<>();
        for (CourseTypeVo courseTypeVo : courseTypeVos) {
            CourseTypeVo courseTypeVo1 = map.get(courseTypeVo.getPid());
            if (courseTypeVo1 != null) {
                courseTypeVo1.getChild().add(courseTypeVo);
            } else {
                courseTypeVoList.add(courseTypeVo);
            }
        }
        return courseTypeVoList;
    }

    /**
     * @param request
     * @Description:新增课程分类
     * @Author: chenBing
     * @Date: 2022/11/7
     */
    @Override
    public Boolean addCourseType(CourseTypeRequest request) {
        Category c = getOne(new LambdaQueryWrapper<Category>().eq(Category::getType, 8).eq(Category::getPid, 0));
        Category category = new Category();
        BeanUtils.copyProperties(request, category);
        category.setExtra(null);
        category.setType(c.getType());
        category.setPid(c.getId());
        category.setPath("/0/" + c.getId() + "/");
        return save(category);
    }

    /**
     * @param request
     * @Description:修改课程分类
     * @Author: chenBing
     * @Date: 2022/11/7
     */
    @Override
    public Boolean editCourseType(CourseTypeRequest request) {
        Category category = new Category();
        BeanUtils.copyProperties(request, category);
        return updateById(category);
    }

    /**
     * @param cid
     * @Description:删除课程分类
     * @Author: chenBing
     * @Date: 2022/11/7
     */
    @Override
    public Boolean removeCourseType(Integer cid) {
        return update(new UpdateWrapper<Category>().set("is_del", 1).eq("id", cid));
    }

    /**
     * 分页显示商品分类
     *
     * @param status
     * @param productTypeName
     * @author li
     * @since 2022-11-21
     */
    @Override
    public List<ProductTypeAdminResponse> getProductTypeList(Integer status, String productTypeName) {
        List<ProductTypeAdminResponse> responses=new ArrayList<>();
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(status)){
            lambdaQueryWrapper.eq(Category::getStatus, status);
        }
        if (ObjectUtil.isNotEmpty(productTypeName)) {
            lambdaQueryWrapper.like(Category::getName,productTypeName).or().like(Category::getNameEn,productTypeName);
        }
        lambdaQueryWrapper.eq(Category::getType,CategoryConstants.CATEGORY_TYPE_PRODUCT);
        lambdaQueryWrapper.eq(Category::getIsDel,0);
        lambdaQueryWrapper.orderByDesc(Category::getSort).orderByDesc(Category::getId);
        List<Category> categories = dao.selectList(lambdaQueryWrapper);
        if (ObjectUtil.isEmpty(categories)){
            return responses;
        }
        List<ProductTypeAdminResponse> group=new ArrayList<>();
        categories.forEach(e->{
            ProductTypeAdminResponse response=new ProductTypeAdminResponse();
            BeanUtils.copyProperties(e,response);
            response.setProductTypeName(e.getName());
            response.setProductTypeNameEn(e.getNameEn());
            response.setLabelColor(e.getLabelColor());
            group.add(response);
        });
        //根据父id进行分组
        List<ProductTypeAdminResponse> filter = group.stream().filter((ProductTypeAdminResponse type) -> type.getPid() != 0).collect(Collectors.toList());
        Map<Integer, List<ProductTypeAdminResponse>> typeMap = filter.stream().collect(Collectors.groupingBy(e->e.getPid()));
        Map<Integer,ProductTypeAdminResponse> map = new HashMap<>();
        //取出父类
        group.forEach(e->{
            if (e.getPid()==0){
                map.put(e.getId(),e);
            }
        });
        //当查询出的类别不存在父类
        if (ObjectUtil.isEmpty(map)){
            List<Integer> pids = categories.stream().map(Category::getPid).collect(Collectors.toList());
            List<Category> list = dao.selectList(new LambdaQueryWrapper<Category>().in(Category::getId, pids));
            list.forEach(e->{
                ProductTypeAdminResponse response=new ProductTypeAdminResponse();
                BeanUtils.copyProperties(e,response);
                response.setProductTypeName(e.getName());
                response.setProductTypeNameEn(e.getNameEn());
                response.setLabelColor(e.getLabelColor());
                group.add(response);
            });
            //取出父类
            group.forEach(e->{
                if (e.getPid()==0){
                    map.put(e.getId(),e);
                }
            });
        }
        //取出有子类的类别
        //根据父id取出父类并添加到返回列表中
        typeMap.forEach((k,v)->{
            ProductTypeAdminResponse response = map.get(k);
            map.remove(k);
            if (ObjectUtil.isNotEmpty(response)){
                if (ObjectUtil.isNotEmpty(v)){
                    response.setChild(v);
                }
                responses.add(response);
            }
        });
        //将无子类别的添加到返回列表中
        map.forEach((k,v)->{
            ProductTypeAdminResponse response =v;
            if (ObjectUtil.isNotEmpty(response)){
                responses.add(response);
            }
        });
        return responses.stream().sorted(Comparator.comparing(ProductTypeAdminResponse::getSort).reversed().thenComparing(Comparator.comparing(ProductTypeAdminResponse::getId).reversed())).collect(Collectors.toList());
    }

    /**
     * 新增商品类型
     *
     * @param request 新增参数
     * @author Li
     * @date 2022/11/22 09:24
     */
    @Override
    public boolean addProductType(ProductTypeRequest request) {
        Category category = new Category();
        BeanUtils.copyProperties(request,category);
        //检测标题是否存在
        if (checkName(request.getName(), 1) > 0) {
            throw new VcaException("此分类已存在");
        }
        category.setPath(getPathByPId(category.getPid()));
        category.setExtra(systemAttachmentService.clearPrefix(category.getExtra()));
        return transactionTemplate.execute(e->{
            save(category);
            return Boolean.TRUE;
        });
    }

    /**
     * 删除商品分类
     *
     * @param id Integer
     * @author Li
     * @since 2022/11/22 09:31
     */
    @Override
    public boolean deleteProudctType(Integer id) {
        VcaException exception=null;
        switch (id) {
            case 749:
                exception= new VcaException("纪念品分类不可被删除");
                break;
            case 750:
                exception= new VcaException("礼品卡分类不可被删除");
                break;
            case 751:
                exception= new VcaException("出版物分类不可被删除");
                break;
            default:
                break;
        }
        if (ObjectUtil.isNotEmpty(exception)){
            throw exception;
        }
        Category category = dao.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id));
        if (ObjectUtil.isEmpty(category)){
            throw new VcaException("该分类已被删除");
        }
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Category::getPid,id);
        wrapper.eq(Category::getIsDel,0);
        List<Category> categories = dao.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(categories)){
            throw new VcaException("当前分类下还有子分类，请先删除子分类");
        }
        return transactionTemplate.execute(e->{
            update(new UpdateWrapper<Category>().set("is_del", 1).eq("id", id));;
            return Boolean.TRUE;
        });
    }

    /**
     * 修改商品分类
     *
     * @param request 修改参数
     * @author Li
     * @since 2022/11/22 09:46
     */
    @Override
    public boolean updateProductType(ProductTypeRequest request) {
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Category::getId,request.getId());
        Category category = dao.selectOne(wrapper);
        if(ObjectUtil.isEmpty(category)){
            throw new VcaException("该分类不存在");
        }
        if (request.getId()==749||request.getId()==750||request.getId()==751){
            request.setName(category.getName());
            request.setNameEn(category.getNameEn());
        }
        BeanUtils.copyProperties(request,category);
        category.setPath(getPathByPId(category.getPid()));
        category.setExtra(systemAttachmentService.clearPrefix(category.getExtra()));
        return transactionTemplate.execute(e->{
            dao.updateById(category);
            return Boolean.TRUE;
        });
    }
}

